package com.fx.controller;

import com.alibaba.fastjson.JSONObject;
import com.fx.entity.CstInfo;
import com.fx.entity.GoodsInfo;
import com.fx.entity.ShopInfo;
import com.fx.service.CstInfoService;
import com.fx.service.GoodsInfoService;
import com.fx.service.ShopInfoService;
import com.fx.util.Constant;
import com.fx.util.PageInfo;
import com.fx.util.Query;
import com.fx.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yfl
 * @email 617823298@qq.com
 * @date 2018-06-19 16:25:21
 */

@Controller
@RequestMapping("/goodsInfo")
public class GoodsInfoController extends BaseController {
    @Autowired
    private GoodsInfoService goodsInfoService;
    private static String pageTitle = "商品管理";
    private static String pageUrl = "/goodsInfo";
    @Autowired
    private ShopInfoService shopInfoService;
    private CstInfoService cstInfoService;

    @GetMapping()
    String goodsInfo(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.LIST_TITLE));
        return "goodsInfo/goodsInfo";
    }

    @ResponseBody
    @GetMapping("/list")
    public JSONObject list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<GoodsInfo> goodsInfoList = goodsInfoService.getList(query);
        int total = goodsInfoService.getCount(query);
        return renderPage(total, goodsInfoList);
    }

    @GetMapping("/add")
    String add(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.ADD_TITLE));
        return "goodsInfo/add";
    }

    @GetMapping("/edit/{flId}")
    String edit(@PathVariable("flId") String flId, Model model) {
        GoodsInfo goodsInfo = goodsInfoService.getById(flId);
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.EDIT_TITLE));
        model.addAttribute("goodsInfo", goodsInfo);
        return "goodsInfo/edit";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public R saveOrUpdate(GoodsInfo goodsInfo) {
        String id = goodsInfo.getFlId();
        int result = 0;
        if (id == null || "".equals(id)) {
            result = goodsInfoService.save(goodsInfo);
        } else {
            result = goodsInfoService.updateById(goodsInfo);
        }
        if (result != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchDel")
    @ResponseBody
    public R remove(@RequestParam("ids") String[] ids) {
        if (goodsInfoService.batchRemove(ids) != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    /**
     * 方法名称：根据分类获取商品列表
     * 概要说明：根据分类ID获取商品列表
     *
     * @Ather EDZ
     * @Date 2018-6-25 11:24
     * @Param shopId 店铺id
     * @Param categoryId 分类id(查询所有传入-1)
     * @Return
     */
    @GetMapping("/api/getGoodsByCategory/{shopId}/{categoryId}")
    @ResponseBody
    public R getGoodsByCategory(@PathVariable("shopId") String shopId, @PathVariable("categoryId") String categoryId) {
        ShopInfo shopInfo = shopInfoService.getById(shopId);
        Map<String, Object> param = new HashMap<String, Object>();
        if (!"-1".equals(shopInfo.getFlCstId())) {
            CstInfo cstInfo = cstInfoService.getById(shopInfo.getFlCstId());
            param.put("cstId", cstInfo.getFlId());
        } else {
            param.put("cstId", -1);
        }
        if (!"-1".equals(categoryId)) {
            param.put("cId", categoryId);
        }
        List<GoodsInfo> goodsInfoList = goodsInfoService.getListByCategory(param);
        return R.ok(goodsInfoList);
    }

    /**
     * 方法名称：获取商品详情
     * 概要说明：根据商品Id获取商品详情
     *
     * @Ather EDZ
     * @Date 2018-6-25 11:33
     * @Param goodsId 商品【表情】
     * @Return
     */
    @GetMapping("/api/getGoodsDetail/{goodsId}")
    @ResponseBody
    public R getGoodsDetail(@PathVariable("goodsId") String goodsId) {
        return R.ok(goodsInfoService.getById(goodsId));
    }

}
